package com.itheima.reggie.controller;

/*
@date 2022年07月31日11:27
*/

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.commom.R;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.DishDto;
import com.itheima.reggie.entity.DishFlavor;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.DishFlavorService;
import com.itheima.reggie.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/dish")
@Slf4j
public class DishController {
    @Autowired
    private DishService dishService;

    @Autowired
    private DishFlavorService  dishFlavorService;
   @Autowired
    private  CategoryService categoryService;

    /**
     * 新增菜品
     * @param dishDto
     * @return
     */
   @PostMapping
    public R<String> save(@RequestBody DishDto dishDto){
       log.info(dishDto.toString());
       dishService.saveWithFlavor(dishDto);
       return  R.success("新增菜品成功!");
   }

    /**
     * 菜品分页查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
   @GetMapping("/page")
   public  R<Page> page(int page,int pageSize,String name){
       //1.创建分页构造器对象
       Page<Dish> pageInfo=new Page<>(page,pageSize);
       Page<DishDto> dishDtoPage=new Page<>();
       //2.创建条件构造器
       LambdaQueryWrapper<Dish> lqw=new LambdaQueryWrapper<>();
       //3.模糊查询
       lqw.like(StringUtils.isNotEmpty(name),Dish::getName,name);
       //4.设置排序,先根据数量升序ASC,相同情况下再根据降序排序DESC
       lqw.orderByAsc(Dish::getSort).orderByDesc(Dish::getUpdateTime);
       //5.调用page方法
       dishService.page(pageInfo,lqw);
       //6.对象拷贝
       BeanUtils.copyProperties(pageInfo,dishDtoPage,"records");
       List<Dish> records = pageInfo.getRecords();
       //7.stream
       List<DishDto> list=records.stream().map((item)->{
           //创建DishDto对象
           DishDto dishDto = new DishDto();
           //拷贝
           BeanUtils.copyProperties(item,dishDto);
           //分类Id
           Long categoryId = item.getCategoryId();
           //根据id查询分类对象
           Category category = categoryService.getById(categoryId);
           //判断查询到的分类中不为空
           if(category!=null){
               String categoryName = category.getName();
               dishDto.setCategoryName(categoryName);
           }
            return  dishDto;
       }).collect(Collectors.toList());//转集合
        dishDtoPage.setRecords(list);

       return R.success(dishDtoPage);
   }

   @GetMapping("/{id}")
   public R<DishDto> get(@PathVariable Long id){
       DishDto dishdto = dishService.getByIdWithFlavor(id);
       return R.success(dishdto);
   }

    /**
     * 修改
     * @param dishDto
     * @return
     */
   @PutMapping
   public  R<String> update(@RequestBody DishDto dishDto){
       log.info(dishDto.toString());
       dishService.updateWithFlavor(dishDto);
        return R.success("修改成功!");
   }

    /**
     * 根据条件查询对应的菜品信息
     * @param dish
     * @return
     */
/*   @GetMapping("/list")
   public R<List<Dish>> list(Dish dish){
       //1.构造条件查询器
      LambdaQueryWrapper<Dish> lqw=new LambdaQueryWrapper<>();
      //2.分类是否为空
       lqw.eq(dish.getCategoryId()!=null,Dish::getCategoryId,dish.getCategoryId());
      //3.查看状态启售
       lqw.eq(Dish::getStatus,1);
       //4.排序 先根据sort,相同情况下,再根据updateTime;
       lqw.orderByAsc(Dish::getSort).orderByDesc(Dish::getUpdateTime);
       //5.调用dishService查
       List<Dish> list = dishService.list(lqw);
       return  R.success(list);
   }*/
    //移动端需求展示菜品的口味,方法增强
    @GetMapping("/list")
    public R<List<DishDto>> list(Dish dish){
        //1.构建条件构造器
        LambdaQueryWrapper<Dish> lqw=new LambdaQueryWrapper<>();
        //2.分类是否为空
        lqw.eq(dish.getCategoryId()!=null,Dish::getCategoryId,dish.getCategoryId());
        //3.查看状态的启售
        lqw.eq(Dish::getStatus,1);
        //4.调用DishService
        List<Dish> list = dishService.list(lqw);
        //5.排序 先根据sort,相同情况下,再根据updateTime;
        lqw.orderByAsc(Dish::getSort).orderByDesc(Dish::getUpdateTime);
        //6遍历拷贝
        List<DishDto> resultList=list.stream().map((item)->{
            DishDto dishDto = new DishDto();
            //填充dishDto
            BeanUtils.copyProperties(item,dishDto);
            //查询当前菜品的信息通过id
            Long dishId = item.getId();
            //创建构造器
            LambdaQueryWrapper<DishFlavor> lqw1=new LambdaQueryWrapper<>();
            //比较菜品的id
            lqw1.eq(DishFlavor::getDishId,dishId);
            //查询dishFlavor
            List<DishFlavor> dishFlavorList = dishFlavorService.list(lqw1);
            //设置进去
            dishDto.setFlavors(dishFlavorList);
            return  dishDto;
        }).collect(Collectors.toList());

        return  R.success(resultList);
    }



}
